package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ManagerRoleValueBean;
import com.zhuoshi.utils.DbHelper;

/**
 * 权限表
 * @author telnet
 *
 */
public class ManagerRoleValueDao {
	
	public List<ManagerRoleValueBean> getList(int role_id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		
		List<ManagerRoleValueBean> list = new ArrayList<ManagerRoleValueBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_manager_role_value where 1=1 ");
			sql.append(" and role_id=?");
			sql.append(" and action_type='SHOW'");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, role_id);
			rs = pstat.executeQuery();
			while(rs.next()){
				ManagerRoleValueBean bean = new ManagerRoleValueBean();
				bean.setId(rs.getInt("id"));
				bean.setRole_id(rs.getInt("role_id"));
				bean.setNav_id(rs.getInt("nav_id"));
				bean.setAction_type(rs.getString("action_type"));
				list.add(bean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		
		
		return list;
	}
	 
	public int delete(int role_id){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from mt_manager_role_value where role_id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, role_id);
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	public int add(ManagerRoleValueBean bean){
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into mt_manager_role_value (role_id,nav_id,action_type) values");
			sql.append(" (?,?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, bean.getRole_id());
			pstat.setInt(2, bean.getNav_id());
			pstat.setString(3, bean.getAction_type());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}
	
	@SuppressWarnings("null")
	//暂时有毛病  先不能用 逻辑有点问题
	public int updateAuth(ManagerRoleValueBean bean,int role_id){
		Connection conn = null;
		Statement pstat = null;
		int rows = 0;
		
		try {
			conn = DbHelper.getConnection();
			conn.setAutoCommit(false);
			StringBuffer sql1 = new StringBuffer();
			sql1.append("delete from mt_manager_role_value where role_id='"+role_id+"'");
			pstat.execute(sql1.toString());
			StringBuffer sql = new StringBuffer();
			sql.append("insert into mt_manager_role_value (role_id,nav_id,action_type) values");
			sql.append(" ('"+bean.getRole_id()+"','"+bean.getNav_id()+"','"+bean.getAction_type()+"')");
			pstat.execute(sql.toString());
			conn.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			DbHelper.dispose(conn, pstat, null);
		}
		
		return rows;
	}

}
